package com.example.demo.controller;

import com.example.demo.admin.dto.SearchFiles;
import com.example.demo.admin.pojo.PageResult;
import com.example.demo.admin.pojo.Result;
import com.example.demo.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
@Slf4j
@Api(tags = "文件管理接口")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ApiOperation("上传文件接口")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传参数{}", file);
        String filePath = uploadService.addfile(file);
        return Result.success(filePath);
    }

    //查看文件列表
    @GetMapping("/lookFilelist")
    @ApiOperation("查看文件列表接口")
    public Result<PageResult> getFileList(SearchFiles searchFiles) {
        log.info("查找文件参数{}",searchFiles);
        PageResult pageResult =  uploadService.list(searchFiles);
        return Result.success(pageResult);
    }
    /**
     * 删除文件
     */

    @DeleteMapping("/deleteFile")
    @ApiOperation("删除文件接口")
    public Result deleteFiles(String url){
        log.info("删除文件{}",url);
        uploadService.deleteFiles(url);
        return Result.success();
    }

}
